<?php

namespace app\api\controller;

use app\api\controller\Base;
use app\common\library\Sms;

/**
 * 个人中心接口
 */
class Usercenter extends Base
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 个人中心
     */
    public function index()
    {
       $data['username'] = $this->user['username'];
       $data['avatar'] = $this->user['avatar'];
       $data['phone'] = $this->user['phone'];
       $data['gold'] = $this->user['gold'];
       $data['silver'] = $this->user['silver'];
       $this->success('操作成功',$data);
    }
    /**
     * 我的交易
     */
    public function myorder(){
        $status = input('status');
        $whereOr['buy_id'] = $this->user['id'];
        $whereOr['sell_id'] = $this->user['id'];
        // $count = db('Order')->field('id,order_num,addtime,price,gold_num,buy_id,sell_id,type,status')->whereOr($whereOr)->count();
        //页
        $page = input('page')?input('page'):1;
        //一页显示数量
        $limit = input('limit')?input('limit'):10;
        $order = db('Order')->field('id,order_num,addtime,price,gold_num,buy_id,sell_id,type,status')->whereOr($whereOr)->order('addtime desc')->page($page,$limit)->select();
        $ordermsg = [];
        foreach($order as $k=>$v){
            $order[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            if($status){
                if($v['status']==$status){
                    $ordermsg[] = $order[$k];
                } 
            }else{
                $ordermsg[] = $order[$k];
            }
        }
        $data['order'] = $ordermsg;
        $data['count'] = count($ordermsg);
        $this->success('操作成功',$data);
    }
    /**
     * 交易记录
     */
    public function transactionRecord(){
        $status = 4;
        $whereOr['buy_id'] = $this->user['id'];
        $whereOr['sell_id'] = $this->user['id'];
        // $count = db('Order')->field('id,order_num,addtime,price,gold_num,buy_id,sell_id,type,status')->whereOr($whereOr)->count();
        //页
        $page = input('page')?input('page'):1;
        //一页显示数量
        $limit = input('limit')?input('limit'):10;
        $order = db('Order')->field('id,order_num,addtime,price,gold_num,buy_id,sell_id,type,status')->whereOr($whereOr)->order('addtime desc')->page($page,$limit)->select();
        $ordermsg = [];
        foreach($order as $k=>$v){
            $order[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            $order[$k]['money'] = $v['price']*$v['gold_num'];
            if($status){
                if($v['status']==$status){
                    $ordermsg[] = $order[$k];
                } 
            }else{
                $ordermsg[] = $order[$k];
            }
        }
        $data['order'] = $ordermsg;
        $data['count'] = count($ordermsg);
        $this->success('操作成功',$data);
    }
    /**
     * 提现
     */
    public function withdrawal(){
        $num = input('num');
        if($num<=0){
            $this->error('数量错误');
        }
        //判断数量是否足够
        if($num*100>$this->user['gold']){
            $this->error('数量不足');
        }
        //类型
        $type = input('type')?input('type'):1;
        //支付密码
        $paypass = input('paypass');
        if(empty($paypass)){
            $this->error('请输入支付密码');
        }
        //判断支付密码是否正确
        if($this->user['paypass'] != md5($paypass)){
            $this->error('支付密码不正确');
        }
        $data['num'] = $num*100;
        $data['d_num'] = $num*90;//扣10%手续费
        $data['type'] = $type;
        $data['user_id'] = $this->user['id'];
        $data['createtime'] = time();
        $r = db('user_withdrawal')->insert($data);
        //扣除金币
        db('user')->where(['id'=>$this->user['id']])->setDec('gold',$num*100);
        //加入财务日志
        $this->addfinancial($this->user['id'],1,'提现',2,$num*100);
        if($r){
            $this->success('提现成功');
        }else{
            $this->error('提现失败');
        }
    }
    /**
     * 我的金币
     */
    public function mygold(){
        $data = $this->user['gold'];
        $this->success('操作成功',$data);
    }
    /**
     * 我的银币
     */
    public function mysilver(){
        $data = $this->user['silver'];
        $this->success('操作成功',$data);
    }
    /**
     * 账单明细
     */
    public function billList(){
        $type = input('type')?input('type'):1;
        $where['user_id'] = $this->user['id'];
        if($type==1){
            $where['status'] = ['in','2,3,4,5,7'];
        }else{
            $where['status'] = ['in','5'];
        }
       
        $count = db('user_log')->field('glod_num,desc,type,createtime,status,silver_num')->where($where)->count();
         //页
        $page = input('page')?input('page'):1;
        //一页显示数量
        $limit = input('limit')?input('limit'):10;
        $list = db('user_log')->field('glod_num,desc,type,createtime,status,silver_num')->where($where)->order('createtime desc')->page($page,$limit)->select();
        foreach($list as $k=>$v){
            $list[$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            if($v['type']==1){
                $list[$k]['type'] = 2;
            }
            if($v['type']==2){
                $list[$k]['type'] = 1;
            }
        }
        $data['count'] =$count;
        $data['list'] = $list;
        $this->success('操作成功',$data);

    }
    /**
     * 团队信息
     */
    public function myteam(){
        $where['first'] = $this->user['id'];
         //页
        $page = input('page')?input('page'):1;
        //一页显示数量
        $limit = input('limit')?input('limit'):10;
        $count = db('user')->field('id,phone,username')->where($where)->count();
        $list = db('user')->field('id,phone,username,avatar')->where($where)->page($page,$limit)->select();
        $data['count'] = $count;
        $data['list'] = $list;
        $this->success('操作成功',$data);
    }
    /**
     * 矿机信息
     */
    public function myminerslist(){
        $where['user_id'] = $this->user['id'];
        //页
        $page = input('page')?input('page'):1;
        //一页显示数量
        $limit = input('limit')?input('limit'):10;
        $count = db('user_miners')->where($where)->order('status')->count();
        $list = db('user_miners')->field('id,miners_id,user_id,status,buy_time,end_time')->where($where)->order('status')->select();
        foreach($list as $k=>$v){
            $miners = db('miners')->where(['id'=>$v['miners_id']])->find();
            $list[$k]['name'] = $miners['name'];
            $list[$k]['image'] = $miners['image'];
            $list[$k]['ability'] = ceil(($miners['production_jin']+$miners['production_yin'])/$miners['length']);
            if($v['status']==1){
               $list[$k]['time'] = '剩余使用期限'.floor(($v['end_time']-time())/(60*60*24)).'天';
            }
            if($v['status']==99){
               $list[$k]['time'] = '已过期';
            }
            if($v['status']==3){
               $list[$k]['time'] = '永久';
            }
        }
        $data['count'] = $count;
        $data['list'] = $list;
        $this->success('操作成功',$data);
    }
    /**
     * 设置页面
     */
    public function setting(){
        //手机号
        $data['phone'] = $this->user['phone'];
        //真实姓名
        $data['truename'] = $this->user['truename'];
        //微信账号
        $data['wx_num'] = $this->user['wx_num'];
        //支付宝账号
        $data['zfb_num'] = $this->user['zfb_num'];
        //银行卡
        $data['card_num'] = substr_replace($this->user['card_num'],'****',12,4);
        $this->success('操作成功',$data);
    }
    /**
     * 修改手机号
     */
    public function setPhone(){
        //原手机号
        $oldphone = input('oldphone');
        if(empty($oldphone)){
            $this->error('原手机号不能空');
        }
        //验证码
        $oldcode = input('oldcode');
        if(empty($oldcode)){
            $this->error('原手机号验证码不能空');
        }
        //新手机号
        $newphone = input('newphone');
        if(empty($newphone)){
            $this->error('新手机号不能空');
        }
        //新验证码
        $newcode = input('newcode');
        if(empty($newcode)){
            $this->error('新手机号验证码不能空');
        }
        //验证验证码
        if (!Sms::check($oldphone,$oldcode))
        {
            $this->error('原手机号验证码不正确');
        }
        //验证验证码
        if (!Sms::check($newphone,$newcode))
        {
            $this->error('新手机号验证码不正确');
        }
        //更改手机号
        $data['phone'] = $newphone;
        $r = db('user')->where(['id'=>$this->user['id']])->update($data);
        if($r){
            $this->success('更改成功');
        }else{
            $this->error('更改失败');
        }
    }
    /**
     * 修改登录密码
     */
    public function setloginpass(){
        //手机号
        $phone = input('phone');
        if(empty($phone)){
            $this->error('手机号不能空');
        }
        //验证码
        $code = input('code');
        if(empty($code)){
            $this->error('验证码不能空');
        }
        //验证验证码
        if (!Sms::check($phone,$code))
        {
            $this->error('验证码不正确');
        }
        //新密码
        $newpass = input('newpass');
        if(empty($newpass)){
            $this->error('请输入新密码');
        }
        //确认新密码
        $confirmpass = input('confirmpass');
        if(empty($confirmpass)){
            $this->error('请输入确认新密码');
        }
        $data['pass'] = md5($newpass);
        $r = db('user')->where(['id'=>$this->user['id']])->update($data);
        if($r){
            $this->success('更改成功');
        }else{
            $this->error('更改失败');
        }
    }
    /**
     * 修改交易密码
     */
    public function setpaypass(){
        //手机号
        $phone = input('phone');
        if(empty($phone)){
            $this->error('手机号不能空');
        }
        //验证码
        $code = input('code');
        if(empty($code)){
            $this->error('验证码不能空');
        }
        //验证验证码
        if (!Sms::check($phone,$code))
        {
            $this->error('验证码不正确');
        }
        //新密码
        $newpass = input('newpass');
        if(empty($newpass)){
            $this->error('请输入新密码');
        }
        //确认新密码
        $confirmpass = input('confirmpass');
        if(empty($confirmpass)){
            $this->error('请输入确认新密码');
        }
        $data['paypass'] = md5($newpass);
        $r = db('user')->where(['id'=>$this->user['id']])->update($data);
        if($r){
            $this->success('更改成功');
        }else{
            $this->error('更改失败');
        }
    }
    /**
     * 实名认证页面
     */
    public function realnamepage(){
        $data['truename'] = $this->user['truename'];
        $data['idcard'] = $this->user['idcard'];
        $this->success('操作成功',$data);
    }
    /**
     * 实名认证方法
     */
    public function realname(){
        //姓名
        $truename = input('truename');
        if(empty($truename)){
            $this->error('请输入姓名');
        }
        //身份证号
        $idcard = input('idcard');
        if(empty($idcard)){
            $this->error('请输入身份证号');
        }
        $data['truename'] = $truename;
        $data['idcard'] = $idcard;
        $r = db('user')->where(['id'=>$this->user['id']])->update($data);
        if($r){
            $this->success('认证成功');
        }else{
            $this->error('认证失败');
        }
    }
    /**
     * 微信认证页面
     */
    public function wxrealpage(){
        $data['wx_num'] = $this->user['wx_num'];
        $data['wx_payimage'] = $this->user['wx_payimage'];
        $this->success('操作成功',$data);
    }
    /**
     * 微信认证方法
     */
    public function wxreal(){
        //微信号
        $wx_num = input('wx_num');
        if(empty($wx_num)){
            $this->error('请输入微信号');
        }
        //微信收款二维码
        $wx_payimage = input('wx_payimage');
        if(empty($wx_payimage)){
            $this->error('请上传微信收款二维码');
        }
        $data['wx_num'] = $wx_num;
        $data['wx_payimage'] = $wx_payimage;
        $r = db('user')->where(['id'=>$this->user['id']])->update($data);
        if($r){
            $this->success('认证成功');
        }else{
            $this->error('认证失败');
        }
    }
    /**
     * 支付宝认证页面
     */
    public function zfbrealpage(){
        $data['zfb_num'] = $this->user['zfb_num'];
        $data['zfb_payimage'] = $this->user['zfb_payimage'];
        $this->success('操作成功',$data);
    }
    /**
     * 支付宝认证方法
     */
    public function zfbreal(){
        //支付宝号
        $zfb_num = input('zfb_num');
        if(empty($zfb_num)){
            $this->error('请输入支付宝号');
        }
        //支付宝收款二维码
        $zfb_payimage = input('zfb_payimage');
        if(empty($zfb_payimage)){
            $this->error('请上传支付宝收款二维码');
        }
        $data['zfb_num'] = $zfb_num;
        $data['zfb_payimage'] = $zfb_payimage;
        $r = db('user')->where(['id'=>$this->user['id']])->update($data);
        if($r){
            $this->success('认证成功');
        }else{
            $this->error('认证失败');
        }
    }
    /**
     * 银行卡认证页面
     */
    public function bankrealpage(){
        $data['card_name'] = $this->user['card_name'];
        $data['card_num'] = $this->user['card_num'];
        $data['card_bank'] = $this->user['card_bank'];
        $this->success('操作成功',$data);
    }
    /**
     * 银行卡认证方法
     */
    public function bankreal(){
        //银行卡真实姓名
        $card_name = input('card_name');
        if(empty($card_name)){
            $this->error('请输入银行卡真实姓名');
        }
        //银行卡号
        $card_num = input('card_num');
        if(empty($card_num)){
            $this->error('请输入银行卡号');
        }
        //开户行
        $card_bank = input('card_bank');
        if(empty($card_bank)){
            $this->error('请上传开户行');
        }
        $data['card_bank'] = $card_bank;
        $data['card_name'] = $card_name;
        $data['card_num'] = $card_num;
        $r = db('user')->where(['id'=>$this->user['id']])->update($data);
        if($r){
            $this->success('认证成功');
        }else{
            $this->error('认证失败');
        }
    }
    /**
     * 关于我们
     */
    public function aboutus(){
        $data['about'] = config('site.aboutus');
        $this->success('操作成功',$data);
    }
    /**
     * 个人信息页面
     */
    public function usermsg(){
        $data['avatar'] = $this->user['avatar'];
        $data['username'] = $this->user['username'];
        $this->success('操作成功',$data);
    }
    /**
     * 修改头像
     */
    public function setavatar(){
        $avatar = input('avatar');
        if(empty($avatar)){
            $this->error('请上传头像');
        }
        $data['avatar'] = $avatar;
        $r = db('user')->where(['id'=>$this->user['id']])->update($data);
        if($r){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    /**
     * 修改昵称
     */
    public function setusername(){
        $username = input('username');
        if(empty($username)){
            $this->error('请输入昵称');
        }
        $data['username'] = $username;
        $r = db('user')->where(['id'=>$this->user['id']])->update($data);
        if($r){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    /**
     * 团队页面
     */
    public function teampage(){
        $where['home'] = ['like','%'.$this->user['id'].'%'];
        $where['delstatus'] = 1;
        $teamcount = db('user')->where($where)->count();
        unset($where['home']);
        $where['first'] = $this->user['id'];
        $count = db('user')->where($where)->count();
        $data['teamcount'] = $teamcount;
        $data['count'] = $count;
        $this->success('操作成功',$data);
    }
    /**
     * 推广页面
     */
    public function tuipage(){
        $data['tuicode'] = $this->user['id'];
        $data['appimage'] = config('site.appimage');
        $this->success('操作成功',$data);
    }

}
